package cn.itsource.pethome.org.service.impl;

import cn.itsource.pethome.org.domain.Department;
import cn.itsource.pethome.org.mapper.DepartmentMapper;
import cn.itsource.pethome.org.service.IDepartmentService;
import cn.itsource.pethome.org.query.DepartmentQuery;
import cn.itsource.pethome.Basic.util.PageList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 部门service实现类
 */
@Service
@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
public class DepartmentServiceImpl implements IDepartmentService {
    @Autowired
    private DepartmentMapper departmentMapper;
    /**
     * 增加部门
     * @param department //部门
     */
    @Override
    @Transactional
    public void save(Department department) {
        departmentMapper.save(department);
    }
    /**
     * 修改部门
     * @param department//部门
     */
    @Override
    @Transactional
    public void update(Department department) {
        departmentMapper.update(department);
    }
    /**
     * 根据id删除
     * @param id //主键id
     */
    @Override
    @Transactional
    public void delete(Long id) {
        departmentMapper.delete(id);
    }
    /**
     * 根据id查询
     * @param id //主键id
     * @return
     */
    @Override
    public Department findById(Long id) {
        return departmentMapper.findById(id);
    }
    /**
     * 查询所有
     * @return
     */
    @Override
    public List<Department> findAll() {
        return departmentMapper.findAll();
    }

    /**
     * 查根据条件查询列表
     * @param departmentQuery 查询条件
     * @return
     */
    @Override
    public PageList<Department> findByQuery(DepartmentQuery departmentQuery) {
        //根据条件获取总条数
        Long total = departmentMapper.findCount(departmentQuery);
        //判断总条数
        if(total.equals(0L)){
            return new PageList<Department>();
        }
        //根据条件查询列表
        List<Department> data = departmentMapper.findByQuery(departmentQuery);
        //封装到PageDepartmentList
        return new PageList<Department>(data,total);
    }

    /**
     * 批量删除部门
     * @param ids
     */
    @Override
    public void deleteIds(Long[] ids) {
        for (Long id : ids) {
            departmentMapper.delete(id);
        }
    }
}
